home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / arc.zoo / tmclock.c < prev    next >
C/C++ Source or Header  |  1988-11-16  |  2KB  |  104 lines

  1. /*
  2.  * Stolen from Jef Poskanzer's tws time library, which was stolen from
  3.  * Marshall Rose's MH Message Handling system...
  4.  *
  5.  * tmclock() will convert time from a tm struct back to a clock value.
  6.  * tmjuliandate() converts a tm struct to its julian day number.
  7.  * tmsubdayclock() takes hours, minutes, and seconds from a tm struct
  8.  * and returns the number of seconds since midnight of that day. (?)
  9.  *  -- Howard Chu, August 1 1988      hyc@umix.cc.umich.edu, umix!hyc
  10.  */
  11.  
  12. /* $Header: tmclock.c,v 1.3 88/08/02 14:15:58 hyc Exp $ */
  13.  
  14. /* Julian day number of the Unix* clock's origin, 01 Jan 1970. */
  15. #define JD1970 2440587L
  16. #define    CENTURY    19
  17. #if    BSD
  18. #include <sys/time.h>
  19. int    daylight;
  20. #else
  21. #include <time.h>
  22. #endif
  23.  
  24. long    tzone;
  25.  
  26. long
  27. tmjuliandate( tm )
  28. struct tm *tm;
  29.     {
  30.     register int mday, mon, year;
  31.     register long a, b;
  32.     double jd;
  33.  
  34.     if ( (mday = tm -> tm_mday) < 1 || mday > 31 ||
  35.         (mon = tm -> tm_mon + 1) < 1 || mon > 12 ||
  36.         (year = tm -> tm_year) < 1 || year > 10000 )
  37.     return ( -1L );
  38.     if ( year < 100 )
  39.     year += CENTURY * 100;
  40.  
  41.     if ( mon == 1 || mon == 2 )
  42.     {
  43.     --year;
  44.     mon += 12;
  45.     }
  46.     if ( year < 1583 )
  47.     return ( -1L );
  48.     a = year / 100;
  49.     b = 2 - a + a / 4;
  50.     b += (long) ( (double) year * 365.25 );
  51.     b += (long) ( 30.6001 * ( (double) mon + 1.0 ) );
  52.     jd = mday + b + 1720994.5;
  53.     return ( (long) jd );
  54.     }
  55.  
  56.  
  57. long
  58. tmsubdayclock( tm )
  59. struct tm *tm;
  60.     {
  61.     register int sec, min, hour;
  62.     register long result;
  63. #if    BSD
  64.     {
  65.        struct timeval tp;
  66.        struct timezone tzp;
  67.  
  68.        gettimeofday(&tp, &tzp);
  69.        daylight=tzp.tz_dsttime;
  70.        tzone=tzp.tz_minuteswest*(-60);
  71.     }
  72. #else
  73.     tzone=timezone;    /* declared as extern in SYSV <time.h> */
  74. #endif
  75.     if ( (sec = tm -> tm_sec) < 0 || sec > 59 ||
  76.         (min = tm -> tm_min) < 0 || min > 59 ||
  77.         (hour = tm -> tm_hour) < 0 || hour > 23 )
  78.     return ( -1L );
  79.  
  80.     result = ( hour * 60 + min ) * 60 + sec;
  81.     result -= tzone;
  82.     if ( daylight )
  83.     result -= 60 * 60;
  84.  
  85.     return ( result );
  86.     }
  87.  
  88.  
  89. long
  90. tmclock( tm )
  91. struct tm *tm;
  92.     {
  93.     register long jd, sdc, result;
  94.  
  95.     if ( ( jd = tmjuliandate( tm ) ) == -1L )
  96.     return ( -1L );
  97.     if ( ( sdc = tmsubdayclock( tm ) ) == -1L )
  98.     return ( -1L );
  99.  
  100.     result = ( jd - JD1970 ) * 24 * 60 * 60 + sdc;
  101.  
  102.     return ( result );
  103.     }
  104.